package com.yuwei.home

import android.util.Log
import android.widget.TextView
import androidx.lifecycle.lifecycleScope
import androidx.recyclerview.widget.LinearLayoutManager
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.blankj.utilcode.util.LogUtils
import com.blankj.utilcode.util.ToastUtils
import com.scwang.smart.refresh.layout.api.RefreshLayout
import com.scwang.smart.refresh.layout.listener.OnRefreshLoadMoreListener
import com.yuwei.base.RoutePath
import com.yuwei.common.mvi.BaseMviFragment
import com.yuwei.common.mvi.UiState
import com.yuwei.home.adapter.SimpleTypeAdapter
import com.yuwei.home.adapter.SimpleVideoAdapter
import com.yuwei.home.commentdialog.CommentDialog
import com.yuwei.home.databinding.FragmentHomeBinding
import com.yuwei.home.entity.SimpleTypeEntity
import com.yuwei.common.entity.SimpleVideoEntity
import com.yuwei.home.adapter.PagingAdapter
import kotlinx.coroutines.flow.collectIndexed
import kotlinx.coroutines.launch

@Route(path = RoutePath.HOME_FRAGMENT)
class HomeFragment : BaseMviFragment<FragmentHomeBinding, HomeViewModel>() {
    private val simpleAdapter = SimpleTypeAdapter()
    private val videoAdapter = SimpleVideoAdapter()
    private var page = 0
    private var mPosition = 0
    private lateinit var commentDialog: CommentDialog

    //    private val pagingAdapter = PagingAdapter()
    override fun successData(it: UiState.onSuccess<*>) {
        when (it.type) {
            "getSimpleType" -> {
                simpleAdapter.data.addAll((it.data as SimpleTypeEntity))
                simpleAdapter.notifyDataSetChanged()
            }
            "getSimpleVideoByChannelId" -> {
                LogUtils.d("666666", (it.data as SimpleVideoEntity).size)
                videoAdapter.data.addAll((it.data as SimpleVideoEntity))
                videoAdapter.notifyDataSetChanged()
                binding.srl.finishRefresh()
                binding.srl.finishLoadMore()
            }
        }

    }

    override fun failureData(error: String) {

    }

    override fun initView() {
        vm.sendIntent(HomeIntent.getSimpleType)
        vm.sendIntent(HomeIntent.getSimpleVideoByChannelId("94349546935", 0, 5))
        binding.tbv.setDatas(
            listOf(
                "海绵宝宝和派大星",
                "哥斯拉大战金刚",
                "这是什么行为艺术"
            )
        )
        binding.tbv.startViewAnimator()
        binding.videoTypeRv.adapter = simpleAdapter
        binding.videoTypeRv.layoutManager =
            LinearLayoutManager(requireContext(), LinearLayoutManager.HORIZONTAL, false)
        simpleAdapter.setOnItemClickListener { adapter, view, position ->
            if (position != mPosition) {
                simpleAdapter.setPosition(position)
                simpleAdapter.notifyDataSetChanged()
                mPosition = position
                videoAdapter.data.clear()
                vm.sendIntent(
                    HomeIntent.getSimpleVideoByChannelId(
                        simpleAdapter.data[mPosition].channelid,
                        0,
                        5
                    )
                )
            }

        }
        binding.videoRv.adapter = videoAdapter
        binding.videoRv.layoutManager = LinearLayoutManager(requireContext())
        binding.srl.setOnRefreshLoadMoreListener(object : OnRefreshLoadMoreListener {
            override fun onRefresh(refreshLayout: RefreshLayout) {
                videoAdapter.data.clear()
                page = 0
                vm.sendIntent(
                    HomeIntent.getSimpleVideoByChannelId(
                        simpleAdapter.data[mPosition].channelid,
                        page,
                        5
                    )
                )
            }

            override fun onLoadMore(refreshLayout: RefreshLayout) {
                page += 1
                vm.sendIntent(
                    HomeIntent.getSimpleVideoByChannelId(
                        simpleAdapter.data[mPosition].channelid,
                        page,
                        5
                    )
                )
            }
        })

        videoAdapter.setOnItemClickListener { adapter, view, position ->
            val followNum = view.findViewById<TextView>(R.id.simple_video_follow_num)
            followNum.setOnClickListener {
                commentDialog = CommentDialog(videoAdapter.data[position].item_id)
                commentDialog.show(this@HomeFragment.childFragmentManager, "getCommentList")

            }
            ARouter.getInstance().build(RoutePath.VIDEO_HOME_PLAY_DETAIL)
                .withSerializable("homeVideo", videoAdapter.data[position]).navigation()
        }


        //Paging3
//        lifecycleScope.launch {
//            vm.loadSimpleVideo("94349546935").collectIndexed { index, value ->
//                LogUtils.d("666666",index)
//                pagingAdapter.submitData(value)
//            }
//        }
//        binding.videoRv.adapter = pagingAdapter




    }

    override fun onDestroyView() {
        commentDialog.dismiss()
        commentDialog.onDestroyView()
        super.onDestroyView()

    }


    override fun initData() {

    }

}